package com.taotao.service.impl;

import com.taotao.common.pojo.BaseResult;
import com.taotao.common.utils.IpUtil;
import com.taotao.common.wxpay.MyWXConfig;
import com.taotao.common.wxpay.WXPay;
import com.taotao.common.wxpay.WXPayUtil;
import com.taotao.mapper.*;
import com.taotao.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class WechatH5PayServiceImpl implements WechatH5PayService {

	private static final Logger logger = LoggerFactory.getLogger(WechatH5PayServiceImpl.class);
	@Autowired
	EyMemberMapper eyMemberMapper;
	@Autowired
	EyGoodsMapper eyGoodsMapper;
	@Autowired
	OperateLogService operateLogService;
	@Autowired
	EyOrderMapper eyOrderMapper;
	@Autowired
	EyOrderWxpayMapper eyOrderWxpayMapper;
	@Autowired
	EyTransferMapper eyTransferMapper;
	@Autowired
	ParamService paramService;
	@Autowired
	ChargeService chargeService;
	@Autowired
	WechatService wechatService;
	@Autowired
	EyServiceMapper eyServiceMapper;
	@Autowired
	EyOfficialAccountsMapper eyOfficialAccountsMapper;
	
	public static String accessToken_xcx_prefix = "accessToken_xcx";
	public static String accessToken_gzh_prefix = "accessToken_gzh";
	public final static String token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
	  
	
	BaseResult baseResult = new BaseResult();


	/**
	 * H5统一下单接口
	 */
	@Override
	public Map<String, Object> unifiedOrder(Map<String, String> map, HttpServletRequest request) {
        try {
			MyWXConfig.init(ParamServiceImpl.getResourceParam("apiclient_cert_path"));
			MyWXConfig config = new MyWXConfig();
			WXPay wxpay = new WXPay(config);

			Map<String, String> data = new HashMap<>();
			data.put("appid", config.getAppID());
			data.put("mch_id", config.getMchID());
			data.put("nonce_str",  WXPayUtil.generateNonceStr());
			data.put("body", "H5订单支付");
			String outTradeNo = "ey" + new Date().getTime();
			data.put("out_trade_no", outTradeNo);//订单号
			data.put("total_fee", "1");//支付金额
			data.put("spbill_create_ip", IpUtil.getIPAddress(request));
			String notify_url = paramService.getParam("wxPayNotify");
			data.put("notify_url",notify_url);
			data.put("trade_type","MWEB");//H5支付的交易类型为MWEB
			String sign = WXPayUtil.generateSignature(data, config.getKey());  //签名
			data.put("sign",sign);
			System.out.println("request unifiedOrderH5-->"+data.toString());
			Map<String, String> response = wxpay.unifiedOrder(data);
			System.out.println("response unifiedOrderH5-->"+response.toString());
			String returnCode = response.get("return_code");
			String mwebUrl="";
			if (returnCode.equals("SUCCESS")) {
				mwebUrl=response.get("mweb_url");
			} else {
				return baseResult.getErrorMap("error");
			}
             return baseResult.getSuccMap(mwebUrl);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
	}

}
